package com.hellacutty.shake;

import java.util.*;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.TouchDelegate;
import android.widget.ImageView;
import android.widget.Toast;

public class CanvasActivity2Player extends Activity {
	/** Called when the activity is first created. */
	private ShakeClass shake;
	private List<CanProvideShakes> players;//represents this device's player and all opponents
	private Handler handler = new Handler();
	private ImageView iv;
	private long start;
	private boolean shouldDraw;
	private Runnable r = new Runnable(){
		@Override
		public void run() {
			// TODO Auto-generated method stub
			iv.invalidate();
		}

	};

	public void setShouldDraw(boolean newShouldDraw) {
		this.shouldDraw = newShouldDraw;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		shouldDraw = true;
		players = new LinkedList<CanProvideShakes>();
		start = System.currentTimeMillis();
		setContentView(R.layout.canvas);
		iv = (ImageView) findViewById(R.id.imageView1);
		iv.setImageDrawable(new Drawable(){
			private Bitmap android = BitmapFactory.decodeResource(getResources(),
					R.drawable.android);
			private Bitmap androidopponent = BitmapFactory.decodeResource(getResources(),
					R.drawable.blue);
			private Bitmap finishLine = BitmapFactory.decodeResource(getResources(),
					R.drawable.finish);
			public void draw(Canvas canvas) {
//				if(shouldDraw){
					// TODO Auto-generated method stub
					canvas.drawColor(Color.BLACK);
					int width = canvas.getWidth();
					while(width>0){
						canvas.drawBitmap(finishLine, canvas.getWidth() - width, canvas.getHeight()-finishLine.getHeight(), null);
						width -= finishLine.getWidth();
					}

					int i = 0;
					for(CanProvideShakes shaker : players){
						switch(i){
						case 0:
							canvas.drawBitmap(android, 100, shaker.getShakeCount(), null);
							break;
						case 1:
							canvas.drawBitmap(androidopponent, canvas.getWidth() - 100 - androidopponent.getWidth(), shaker.getShakeCount(), null);
							break;
						default:
							throw new IllegalStateException("drawing unregistered player");
						}
						i++;
					}



					if(players.get(0).getShakeCount() > iv.getHeight()-android.getHeight()){
						long totalLength = System.currentTimeMillis()-start;
						BluetoothHelper.sendMessage(ProtocolConstants.OPPONENT_WIN, ((ShakeApplication)getApplication()).getBTService());
						Toast.makeText(getApplicationContext(), "YOU WON!", Toast.LENGTH_LONG).show();
						Log.d("finish time", ""+totalLength);
					}
					else if(shouldDraw){
						handler.postDelayed(r, 16);
					}
//				}
//				else{
//					int width = canvas.getWidth();
//					while(width>0){
//						canvas.drawBitmap(finishLine, canvas.getWidth() - width, canvas.getHeight()-finishLine.getHeight(), null);
//						width -= finishLine.getWidth();
//					}
//					CanProvideShakes shaker0 = players.get(0);
//					CanProvideShakes shaker1 = players.get(1);
//					canvas.drawBitmap(android, 100, shaker0.getShakeCount(), null);
//					canvas.drawBitmap(androidopponent, canvas.getWidth() - 100 - androidopponent.getWidth(), shaker1.getShakeCount(), null);
//					
//				}
			}

			@Override
			public int getOpacity() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void setAlpha(int alpha) {
				// TODO Auto-generated method stub
			}
			@Override
			public void setColorFilter(ColorFilter cf) {
				// TODO Auto-generated method stub

			}
		});
		//        shake = new ShakeClass((SensorManager)getSystemService(SENSOR_SERVICE));
		players.add(0, new ShakeClass((SensorManager)getSystemService(SENSOR_SERVICE), ((ShakeApplication)getApplication()).getBTService()));
		//add a new BluetoothOpponentListener with the BTservice so it can listen and update shakes on opponent input
		players.add(new BluetoothOpponentShakeListener(((ShakeApplication)getApplication()).getBTService(), getApplicationContext(), this));

	}
}